package kr.co.cherryt.common;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.util.Log;

public class Utils {
    public static void CopyStream(InputStream is, OutputStream os)
    {
        final int buffer_size=1024;
        try
        {
            byte[] bytes=new byte[buffer_size];
            for(;;)
            {
              int count=is.read(bytes, 0, buffer_size);
              //if(count==-1)
              if(count < 1){
            	  //Log.i("cache", "size:" + count);
                  break;
              }else{
            	  //Log.i("cache", "size:" + count);
              }
              
              os.write(bytes, 0, count);
            }
        }
        catch(Exception ex){
            //Log.i("pf", "wwwwww exception!!");
        }
    }
    
	public static boolean DownloadImageEx(String szKey,String szFileName, String mPath){//이미지 파일 저장
		URL szUrl;
		int Read;
		HttpURLConnection conn = null;
		InputStream is = null;
		FileOutputStream fos = null;
		
		try {
			szUrl = new URL( szKey);
			conn = (HttpURLConnection)szUrl.openConnection();
			int len = conn.getContentLength();
			byte[] raster = new byte[len];
			is = conn.getInputStream();

			fos = new FileOutputStream(mPath + szFileName);

			for(;;){
				Read = is.read(raster);
				if(Read <= 0)
					break;
				fos.write(raster,0,Read);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			try {
				if(is != null)
					is.close();
				if(fos != null)
					fos.close();
			} catch (IOException e) {
			}
			if(conn != null)
				conn.disconnect();
		}

		return true;
	}    
}